const projects: Fig.Generator = {
  script: ["tmuxinator", "list", "-n"],
  postProcess: (output) => {
    if (output.startsWith("fatal:")) {
      return [];
    }
    return output
      .split("\n")
      .splice(1)
      .map((project) => {
        return {
          name: project,
          description: "Project",
        };
      });
  },
};

const tmuxsessions: Fig.Generator = {
  script: ["tmux", "ls"],
  postProcess: (output) => {
    if (output.startsWith("fatal:")) {
      return [];
    }
    return output.split("\n").map((project) => {
      return {
        name: project.substring(0, project.indexOf(":")),
        description: "Tmux Session - " + project,
      };
    });
  },
};

const completionSpec: Fig.Spec = {
  name: "tmuxinator",
  description: "Create and manage tmux sessions easily",
  subcommands: [
    {
      name: "commands",
      description: "Lists commands available in tmuxinator",
    },
    {
      name: "completions",
      description: "Used for shell completion",
    },
    {
      name: "copy",
      description:
        "Copy an existing project to a new project and open it in your editor",
      args: [
        {
          name: "source",
          description: "The source project name",
          generators: projects,
        },
        {
          name: "target",
          description: "The target project name",
          generators: projects,
        },
      ],
    },
    {
      name: "debug",
      description: "Output the shell commands that are generated by tmuxinator",
      options: [
        {
          name: ["-a", "--attach"],
          description: "Attach to tmux session after creation",
          args: {
            name: "attach-session",
            description: "Attach to tmux session",
          },
        },
        {
          name: ["-n", "--name"],
          description: "Give the session a different name",
          args: {
            name: "name",
            description: "Give the session a different name",
          },
        },
        {
          name: ["-p", "--project-config"],
          description: "Path to project config file",
          args: {
            name: "project-config",
            description: "Path to project config file",
            template: ["folders", "filepaths"],
          },
        },
      ],
    },
    {
      name: "delete",
      description: "Deletes given project",
      args: {
        name: "project",
        description: "The project name",
        generators: projects,
        isVariadic: true,
      },
    },
    {
      name: "doctor",
      description: "Look for problems in your configuration",
    },
    {
      name: "help",
      description: "Describe available commands or one specific command",
      args: {
        name: "command",
        description: "The tmuxinator commands",
        template: "help",
      },
    },
    {
      name: "implode",
      description: "Deletes all tmuxinator projects",
    },
    {
      name: "list",
      description: "Lists all tmuxinator projects",
      options: [
        {
          name: ["-n", "--newline"],
          description: "Force output to be one entry per line",
        },
      ],
    },
    {
      name: "local",
      description: "Start a tmux session using ./.tmuxinator.y[a]ml",
      options: [
        {
          name: "--suppress-tmux-version-warning",
          description: "Don't show a warning for unsupported tmux versions",
        },
      ],
    },
    {
      name: "new",
      description: "Create a new project file and open it in your editor",
      args: [
        {
          name: "project",
          description: "The project name",
        },
        {
          name: "tmux-session",
          description: "The tmux session name",
          generators: tmuxsessions,
          isOptional: true,
        },
      ],
      options: [
        {
          name: "-l",
          description: "Create local project file at ./.tmuxinator.y[a]ml",
        },
      ],
    },
    {
      name: "edit",
      description: "Open a project file it in your editor",
      args: {
        name: "project",
        description: "The project name",
        generators: projects,
      },
      options: [
        {
          name: "-l",
          description: "Open local project file at ./.tmuxinator.y[a]ml",
        },
      ],
    },
    {
      name: "open",
      description: "Open a project file it in your editor",
      args: {
        name: "project",
        description: "The project name",
        generators: projects,
      },
      options: [
        {
          name: "-l",
          description: "Open local project file at ./.tmuxinator.y[a]ml",
        },
      ],
    },
    {
      name: "start",
      description:
        "Start a tmux session using a project's name (with an optional [ALIAS] for project reuse) or a path to a project config file (via the -p flag)",
      args: {
        name: "project",
        description: "The project name",
        generators: projects,
        loadSpec: {
          name: "project",
          options: [
            {
              name: ["-a", "--attach"],
              description: "Attach to tmux session after creation",
              args: {
                name: "attach-session",
                description: "Attach to tmux session",
              },
            },
            {
              name: ["-n", "--name"],
              description: "Give the session a different name",
              args: {
                name: "name",
                description: "Give the session a different name",
              },
            },
            {
              name: ["-p", "--project-config"],
              description: "Path to project config file",
              args: {
                name: "project-config",
                description: "Path to project config file",
                template: ["folders", "filepaths"],
              },
            },
            {
              name: "--suppress-tmux-version-warning",
              description: "Don't show a warning for unsupported tmux versions",
            },
          ],
        },
      },
    },
    {
      name: "stop",
      description: "Stop a tmux session using a project's tmuxinator config",
      args: {
        name: "project",
        description: "The project name",
        generators: projects,
        loadSpec: {
          name: "project",
          options: [
            {
              name: "--suppress-tmux-version-warning",
              description: "Don't show a warning for unsupported tmux versions",
            },
          ],
        },
      },
    },
    {
      name: "version",
      description: "Display installed tmuxinator version",
    },
  ],
};

export default completionSpec;
